package com.dunka.demo.exception;

import com.dunka.demo.util.RestResponseUtil;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * @Author Dunka
 * @Description 统一异常处理
 * @Date 10:29   2019/7/29
 * @ClassName ExceptionHandler
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object invalidFormatExceptionHandler(HttpServletRequest request,
                                                Exception e)throws Exception{
        if (e instanceof InvalidFormatException ){
            return RestResponseUtil.invalid("无效格式错误");
        }
        return RestResponseUtil.invalid(e.getMessage().toString());
    }
}
